package sql;
import java.sql.*;
public class JDBC_T {
    private static final String DRIVER_NAME = "com.mysql.cj.jdbc.Driver";
    //数据库连接地址
    private static final String URL = "jdbc:mysql://localhost:3306/数据库课设" +
            "?serverTimezone=UTC&useSSL=true";
    //用户名,更换成你自己的用户名，此处为root用户
    private static final String USER_NAME = "root";
    //密码，更换成你自己设定的密码，此处为:admin
    private static final String PASSWORD = "mhcmhc6754";
    private  static Connection conn=null;
    //连接是否成功
//    public boolean IsRightUser (){
//        String sql1 = "SELECT USERNAME FROM USERS";
//
//        String sql2 = "SELECT PASSWORD FROM USERS WHERE USERNAME "
//        return false;
//    }
//
    public Connection getConn(){
        return conn;
    }

    public boolean is_conn() {

        try {
            Class.forName(DRIVER_NAME);
            conn = DriverManager.getConnection(URL, USER_NAME, PASSWORD);
            if (conn == null) return false;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return true;
    }

    public static void main(String[] args) {
        Connection connection = null;
        try{
            Class.forName(DRIVER_NAME);
            connection = DriverManager.getConnection(URL,USER_NAME,PASSWORD);
            String sql = "SELECT Sname FROM stuinfo";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = preparedStatement.executeQuery();
            while(rs.next()){
                System.out.println("用户名"+rs.getString("Sname"));
            }
            rs.close();
            preparedStatement.close();
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(connection!=null){
                try {
                    connection.close();

                }catch (SQLException sqle){
                    sqle.printStackTrace();
                }
            }
        }
    }

}
